package member

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func GetMemberPositionList(c *gin.Context) {
	var data models.MemberPosition
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
	}

	data.MemberWt = c.Request.FormValue("memberWt")
	data.MemberLt = c.Request.FormValue("memberLt")
	data.OpenId = c.Request.FormValue("memberOpenId")
	data.MemberPosition = c.Request.FormValue("memberPosition")

	data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetMemberPosition(c *gin.Context) {
	var data models.MemberPosition
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
	}
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func InsertMemberPosition(c *gin.Context) {
	var data models.MemberPosition
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
	}
	result, err := data.Create()
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func UpdateMemberPositionV2(c *gin.Context) {
	var err error
	data := models.MemberPosition{
		MemberId:       0,
		MemberWt:       "",
		MemberLt:       "",
		OpenId:         "",
		MemberPosition: "",
		DataScope:      "",
		Params:         "",
		BaseModel:      models.BaseModel{},
	}

	data.MemberWt = c.Request.FormValue("memberWt")

	data.MemberLt = c.Request.FormValue("memberLt")
	data.OpenId = c.Request.FormValue("openId")
	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
		if data.OpenId == "" {
			app.ApiResp(c, -1, "当前用户未获取到openId", nil)
			return
		}
	}

	//先查询 是否存在 不存在则创建
	info, _ := data.Get()
	if info.Id == 0 {
		newInfo, err := data.Create()
		if err != nil {
			app.ApiResp(c, -1, "创建信息失败"+err.Error(), data)
			return
		}
		data.Id = newInfo.Id
		app.OK(c, newInfo, "上报位置成功")
		return
	}

	result, err := data.Update(data.Id)
	if err != nil {
		app.ApiResp(c, -1, "位置更新失败2:"+err.Error(), data)
		return
	}

	app.OK(c, result, "上报位置成功")
}
func UpdateMemberPosition(c *gin.Context) {
	var dataP models.MemberPositionPost
	err := c.BindWith(&dataP, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), dataP)
		return
	}

	var data models.MemberPosition
	data.MemberLt = dataP.MemberLt
	data.MemberWt = dataP.MemberWt
	data.OpenId = dataP.OpenId

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
		if data.OpenId == "" {
			app.ApiResp(c, -1, "当前用户未获取到openId", nil)
			return
		}
	}

	//先查询 是否存在 不存在则创建
	info, err := data.Get()
	if err != nil || info.Id == 0 {
		newInfo, err := data.Create()
		if err != nil {
			app.ApiResp(c, -1, "创建信息失败"+err.Error(), nil)
			return
		}
		data.Id = newInfo.Id
		app.OK(c, newInfo, "上报位置成功")
		return
	}

	result, err := data.Update(info.Id)
	if err != nil {
		app.ApiResp(c, -1, "位置更新失败："+err.Error(), data)
		return
	}

	app.OK(c, result, "上报位置成功")
}

func DeleteMemberPosition(c *gin.Context) {
	var data models.MemberPosition

	IDS := tools.IdsStrToIdsIntGroup("id", c)

	data.MemberId = tools.GetMemberId(c)
	if data.MemberId == 0 {
		data.MemberId = -1
	}
	_, err := data.BatchDelete(IDS)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
